<?php

class AdminApi
{
    private static $controllerCache;

    public function callApi($param)/*{{{*/
    {
        $do =   $param['do'];
        # check web do  /*{{{*/
        $doCheck    =   $this->checkAdminDo($do);
        if (!empty(AC::$errorNo))
        {
            return  $this->formatData($do, $doCheck);
        }/*}}}*/

        # check hostUid /*{{{*/
        $adminUid   =   $this->checkAdminUid($do);
        if (!empty(AC::$errorNo))
        {
            return  $this->formatData($do, $adminUid);
        }/*}}}*/

        $info =   $this->$do($param);
        if (isset($param['format']) &&  'json'  ==  $param['format'])
        {
            $result =   $this->formatData($do, $info);
        }
        else
        {
            $result =   $this->formatHtml($do, $info);
        }
        return  $result;
    }/*}}}*/

    #del all diary fro uid
    private function delAllDiary($param)/*{{{*/
    {
        $where  =   array('uid' => $param['uid']);
        $info       =   Storage::delConditionData('DB/'.$param['uid'],$where);
        $info       =   Storage::delConditionData('DL/'.$param['uid'],$where);
        return $info;
    }/*}}}*/

    # Statistics Dau Api
    private function getSnsDau($param)/*{{{*/
    {
        $controller =   $this->newController('StatisticsController');
        $info       =   $controller->getSnsDau($param);
        return  $info;
    }/*}}}*/
    private function getSnsQuarter($param)/*{{{*/
    {
        $controller =   $this->newController('StatisticsController');
        $info       =   $controller->getSnsQuarter($param);
        return  $info;
    }/*}}}*/
    private function getTotalExchange($param)/*{{{*/
    {
        $controller =   $this->newController('StatisticsController');
        $info       =   $controller->getTotalExchange($param);
        return  $info;
    }/*}}}*/
    private function getDailyExchange($param)/*{{{*/
    {
        $controller =   $this->newController('StatisticsController');
        $info       =   $controller->getDailyExchange($param);
        return  $info;
    }/*}}}*/
    # end Dau Api

    # User Api/*{{{*/
    private function getUserInfo($param)/*{{{*/
    {
        $controller =   $this->newController('UserController');
        $info       =   $controller->getUserInfo($param);
        return  $info;
    }/*}}}*/
    private function adminLogin($param)/*{{{*/
    {
        $controller =   $this->newController('UserController');
        $info       =   $controller->webLogin($param);
        return  $info;
    }/*}}}*/
    private function loginStatus($param)/*{{{*/
    {
        return  DOP::getSession();
    }/*}}}*/
    private function adminLogout($param)/*{{{*/
    {
        return  DOP::setSession(NULL, NULL);
    }/*}}}*/
    private function getUserList($param)/*{{{*/
    {
        $controller =   $this->newController('UserController');
        $info       =   $controller->getUserList($param);
        return  $info;
    }/*}}}*/
    private function getAdminUser($param = NULL)/*{{{*/
    {
        $userInfo   =   array('uid' => DOP::getSession('adminUid'));
        $controller =   $this->newController('UserController');
        $userBase   =   $controller->getUserBaseInfo($userInfo);
        if (empty($userBase['uid']) ||  empty($userBase['nickname']))
        {
            AC::$errorNo    =   'AA001';
            return  ' Error : The Admin userinfo is NULL! ';
        }
        AC::$hostUserBaseInfo   =   $userBase;

        $info   =   $controller->getAdminUser($userInfo);
        return  $info;
    }/*}}}*/
    private function getAdminList($param)/*{{{*/
    {
        $controller =   $this->newController('UserController');
        $info       =   $controller->getAdminList($param);
        return  $info;
    }/*}}}*/
    private function removeAdmin($param)/*{{{*/
    {
        $controller =   $this->newController('UserController');
        $info       =   $controller->removeAdmin($param);
        return  $info;
    }/*}}}*/
    private function fetchUserInfo($param)/*{{{*/
    {
        $controller =   $this->newController('UserController');
        $info       =   $controller->getUserBaseInfoUseLike($param);
        return  $info;
    }/*}}}*/
    private function chainUser($param)/*{{{*/
    {
        $controller =   $this->newController('UserController');
        $info       =   $controller->chainUser($param);
        return  $info;
    }/*}}}*/
    private function modifyUserPasswd($param)/*{{{*/
    {
        $controller =   $this->newController('UserController');
        $info       =   $controller->adminModifyUserLoginPasswd($param);
        return  $info;
    }/*}}}*/
    private function getUserStatistics($param)/*{{{*/
    {
        $controller =   $this->newController('UserStatisticsController');
        $info       =   $controller->getUserStatistics($param);
        return  $info;
    }/*}}}*/
    #   private function unchainUser($param)/*{{{*/
    #   {
    #       $controller =   $this->newController('UserController');
    #       $info       =   $controller->unchainUser($param);
    #       return  $info;
    #   }/*}}}*/
    # end User Api/*}}}*/

    # Diary Api/*{{{*/
    private function getDiaryList($param)/*{{{*/
    {
        $controller =   $this->newController('DiaryController');
        $info       =   $controller->getDiaryList($param);
        return  $info;
    }/*}}}*/
    private function getDiaryInfo($param)/*{{{*/
    {
        $controller =   $this->newController('DiaryController');
        $info       =   $controller->getDiaryInfo($param);
        return  $info;
    }/*}}}*/
    private function writeDiary($param)/*{{{*/
    {
        $param['uid']   =   AC::HOST_UID;
        $controller     =   $this->newController('DiaryController');
        $info           =   $controller->addDiary($param);
        return  $info;
    }/*}}}*/
    private function editDiary($param)/*{{{*/
    {
        $param['uid']   =   AC::HOST_UID;
        $controller     =   $this->newController('DiaryController');
        $info           =   $controller->setDiary($param);
        return  $info;
    }/*}}}*/
    private function removeDiary($param)/*{{{*/
    {
        $param['uid']   =   AC::HOST_UID;
        $controller     =   $this->newController('DiaryController');
        $info           =   $controller->removeDiary($param);
        return  $info;
    }/*}}}*/
    private function fetchDiaryInfo($param)/*{{{*/
    {
        $controller =   $this->newController('HotSquareController');
        $info       =   $controller->getDiaryInfoUseLike($param);
        return  $info;
    }/*}}}*/
    
    private function getDiaryInfoWithCommentReply($param)/*{{{*/
    {
        $controller =   $this->newController('DiaryController');
        $info       =   $controller->getDiaryInfoWithCommentReply($param);
        return  $info;
    }/*}}}*/
    # end Diary Api/*}}}*/

    # Follow Api/*{{{*/
    private function getFollowMeList($param)/*{{{*/
    {
        $controller =   $this->newController('FollowController');
        $info       =   $controller->getFollowMeList($param);
        return  $info;
    }/*}}}*/
    private function getMeFollowList($param)/*{{{*/
    {
        $controller =   $this->newController('FollowController');
        $info       =   $controller->getMeFollowList($param);
        return  $info;
    }/*}}}*/
    private function meFollow($param)/*{{{*/
    {
        $controller =   $this->newController('FollowController');
        $info       =   $controller->meFollow($param);
        return  $info;
    }/*}}}*/
    private function removeFollow($param)/*{{{*/
    {
        $controller =   $this->newController('FollowController');
        $info       =   $controller->removeFollow($param);
        return  $info;
    }/*}}}*/
    private function getBlacklist($param)/*{{{*/
    {
        $controller =   $this->newController('FollowController');
        $info       =   $controller->getBlacklist($param);
        return  $info;
    }/*}}}*/
    private function blacklistFollow($param)/*{{{*/
    {
        $controller =   $this->newController('FollowController');
        $info       =   $controller->blacklistFollow($param);
        return  $info;
    }/*}}}*/
    private function relieveBlacklist($param)/*{{{*/
    {
        $controller =   $this->newController('FollowController');
        $info       =   $controller->relieveBlacklist($param);
        return  $info;
    }/*}}}*/
    # end Follow Api/*}}}*/

    # Feed Api/*{{{*/
    private function getFeedList($param)/*{{{*/
    {
        $controller =   $this->newController('FeedController');
        $info       =   $controller->getFeedList($param);
        return  $info;
    }/*}}}*/
    private function removeFeed($param)/*{{{*/
    {
        $controller =   $this->newController('FeedController');
        $info       =   $controller->removeFeed($param);
        return  $info;
    }/*}}}*/
    private function getTimeline($param)/*{{{*/
    {
        $controller =   $this->newController('FeedController');
        $info       =   $controller->getTimeline($param);
        return  $info;
    }/*}}}*/
    private function getLikesMe($param)/*{{{*/
    {
        $controller =   $this->newController('FeedController');
        $info       =   $controller->getLikesMe($param);
        return  $info;
    }/*}}}*/
    private function getMeLikes($param)/*{{{*/
    {
        $controller =   $this->newController('FeedController');
        $info       =   $controller->getMeLikes($param);
        return  $info;
    }/*}}}*/
    private function getMentionsToMe($param)/*{{{*/
    {
        $controller =   $this->newController('FeedController');
        $info       =   $controller->getMentionsToMe($param);
        return  $info;
    }/*}}}*/
    # end Feed Api/*}}}*/

    # Comment Api/*{{{*/
    private function postComment($param)/*{{{*/
    {
        $controller =   $this->newController('CommentController');
        $info       =   $controller->postComment($param);
        return  $info;
    }/*}}}*/
    private function getCommentMeList($param)/*{{{*/
    {
        $controller =   $this->newController('CommentController');
        $info       =   $controller->getCommentMeList($param);
        return  $info;
    }/*}}}*/
    private function getMeCommentList($param)/*{{{*/
    {
        $controller =   $this->newController('CommentController');
        $info       =   $controller->getMeCommentList($param);
        return  $info;
    }/*}}}*/
    private function removeComment($param)/*{{{*/
    {
        $controller =   $this->newController('CommentController');
        $info       =   $controller->removeComment($param);
        return  $info;
    }/*}}}*/
	
    #删除多个评论          /*{{{*/
    private function removeManyComment($param)
    {
        $controller =   $this->newController('CommentController');
        $info       =   $controller->removeManyComment($param);
        return  $info;
    }/*}}}*/
	
	
    # end Comment Api/*}}}*/

    # Like Api/*{{{*/
    private function postLike($param)/*{{{*/
    {
        $controller =   $this->newController('LikeController');
        $info       =   $controller->postLike($param);
        return  $info;
    }/*}}}*/
    private function getLikeMeList($param)/*{{{*/
    {
        $controller =   $this->newController('LikeController');
        $info       =   $controller->getLikeMeList($param);
        return  $info;
    }/*}}}*/
    private function getMeLikeList($param)/*{{{*/
    {
        $controller =   $this->newController('LikeController');
        $info       =   $controller->getMeLikeList($param);
        return  $info;
    }/*}}}*/
    private function removeLike($param)/*{{{*/
    {
        $controller =   $this->newController('LikeController');
        $info       =   $controller->removeLike($param);
        return  $info;
    }/*}}}*/
    # end Like Api/*}}}*/

    # Cloud Sync Api/*{{{*/
    private function cloudSync($param)/*{{{*/
    {
        $controller     =   $this->newController('CloudSyncController');
        $param['uid']   =   AC::HOST_UID;
        $info           =   $controller->cloudSync($param);
        return  $info;
    }/*}}}*/
    private function cloudTraffic($param)/*{{{*/
    {
        $controller     =   $this->newController('CloudSyncController');
        $param['uid']   =   AC::HOST_UID;
        $info           =   $controller->cloudTraffic($param);
        return  $info;
    }/*}}}*/
    private function cloudSyncCount($param)/*{{{*/
    {
        $controller     =   $this->newController('CloudSyncController');
        $info           =   $controller->cloudSyncCount($param);
        return  $info;
    }/*}}}*/
    private function cloudSyncList($param)/*{{{*/
    {
        $controller     =   $this->newController('CloudSyncController');
        $param['uid']   =   AC::HOST_UID;
        $info           =   $controller->cloudSyncList($param);
        return  $info;
    }/*}}}*/
    private function cloudDownload($param)/*{{{*/
    {
        $controller     =   $this->newController('CloudSyncController');
        $param['uid']   =   AC::HOST_UID;
        $info           =   $controller->cloudDownload($param);
        return  $info;
    }/*}}}*/
    private function cloudUpdate($param)/*{{{*/
    {
        $controller     =   $this->newController('CloudSyncController');
        $param['uid']   =   AC::HOST_UID;
        $info           =   $controller->cloudUpdate($param);
        return  $info;
    }/*}}}*/
    private function cloudRemove($param)/*{{{*/
    {
        $controller     =   $this->newController('CloudSyncController');
        $param['uid']   =   AC::HOST_UID;
        $info           =   $controller->cloudRemove($param);
        return  $info;
    }/*}}}*/
    private function cloudRemoveByUid($param)/*{{{*/
    {
        $controller     =   $this->newController('CloudSyncController');
        $info           =   $controller->cloudRemoveByUid($param);
        return  $info;
    }/*}}}*/
    
    #恢复用户同步信息     /*{{{*/
    private function regainCloudSync($param)
    {
        $controller     =   $this->newController('CloudSyncController');
        $info           =   $controller->regainCloudSync($param);
        return  $info;
    }/*}}}*/
    
    # End Cloud Sync Api/*}}}*/

    # Square, Char, Board, Inform Api/*{{{*/
    private function getSquareList($param)/*{{{*/
    {
        $controller =   $this->newController('HotSquareController');
        $info       =   $controller->getSquareList($param);
        return  $info;
    }/*}}}*/
    private function getSquareLatestUserList($param)/*{{{*/
    {
        $controller =   $this->newController('HotSquareController');
        $info       =   $controller->getSquareLatestUserList($param);
        return  $info;
    }/*}}}*/
    private function removeSquare($param)/*{{{*/
    {
        $controller =   $this->newController('HotSquareController');
        $info       =   $controller->removeSquare($param);
        return  $info;
    }/*}}}*/
    private function postChat($param)/*{{{*/
    {
        $controller =   $this->newController('ChatController');
        $info       =   $controller->postChat($param);
        return  $info;
    }/*}}}*/
    private function getChatList($param)/*{{{*/
    {
        $controller =   $this->newController('ChatController');
        $info       =   $controller->getChatList($param);
        return  $info;
    }/*}}}*/
    private function removeChat($param)/*{{{*/
    {
        $controller =   $this->newController('ChatController');
        $info       =   $controller->removeChat($param);
        return  $info;
    }/*}}}*/
    private function modifyChat($param)/*{{{*/
    {
        $controller =   $this->newController('ChatController');
        $info       =   $controller->modifyChat($param);
        return  $info;
    }/*}}}*/

    private function postBoard($param)/*{{{*/
    {
        $controller =   $this->newController('ChatController');
        $info       =   $controller->postBoard($param);
        return  $info;
    }/*}}}*/
    private function updateBoard($param)/*{{{*/
    {
        $controller =   $this->newController('ChatController');
        $info       =   $controller->updateBoard($param);
        return  $info;
    }/*}}}*/
    private function removeBoard($param)/*{{{*/
    {
        $controller =   $this->newController('ChatController');
        $info       =   $controller->removeBoard($param);
        return  $info;
    }/*}}}*/
    private function getBoardList($param)/*{{{*/
    {
        $param['status']=   TRUE;
        $controller =   $this->newController('BoardController');
        $info       =   $controller->getBoardList($param);
        return  $info;
    }/*}}}*/

    private function postInform($param)/*{{{*/
    {
        $controller =   $this->newController('InformController');
        $info       =   $controller->postInform($param);
        return  $info;
    }/*}}}*/
    private function updateInform($param)/*{{{*/
    {
        $controller =   $this->newController('InformController');
        $info       =   $controller->updateInform($param);
        return  $info;
    }/*}}}*/
    private function removeInform($param)/*{{{*/
    {
        $controller =   $this->newController('InformController');
        $info       =   $controller->removeInform($param);
        return  $info;
    }/*}}}*/
    private function getInformList($param)/*{{{*/
    {
        $controller =   $this->newController('InformController');
        $info       =   $controller->getInformList($param);
        return  $info;
    }/*}}}*/
    # End Square, Char, Board, Inform Api/*}}}*/

    # Web Message Api /*{{{*/
    private function sendMessage($param)/*{{{*/
    {
        $param['uid']   =   AC::HOST_UID;
        $controller     =   $this->newController('MessageBoxController');
        $info           =   $controller->sendMessage($param);
        return  $info;
    }/*}}}*/
    private function removeMessage($param)/*{{{*/
    {
        $param['uid']   =   AC::HOST_UID;
        $controller     =   $this->newController('MessageBoxController');
        $info           =   $controller->removeMessage($param);
        return  $info;
    }/*}}}*/
    private function getMessageList($param)/*{{{*/
    {
        $param['uid']   =   AC::HOST_UID;
        $controller     =   $this->newController('MessageBoxController');
        $info           =   $controller->getMessageList($param);
        return  $info;
    }/*}}}*/
    private function getMessageInfo($param)/*{{{*/
    {
        $param['uid']   =   AC::HOST_UID;
        $controller     =   $this->newController('MessageBoxController');
        $info           =   $controller->getMessageInfo($param);
        return  $info;
    }/*}}}*/
    # End Web Message Api /*}}}*/
    
    # Lbs Api   /*{{{*/
    private function getFriendsLocation($param)/*{{{*/
    {
        $controller     =   $this->newController('LbsController');
        $info           =   $controller->getFriendsLocation($param);
        return  $info;
    }/*}}}*/
    private function getDiaryByLocation($param)/*{{{*/
    {
        $controller     =   $this->newController('LbsController');
        $info           =   $controller->getDiaryByLocation($param);
        return  $info;
    }/*}}}*/
    private function removeUserLbsInfo($param)/*{{{*/
    {
        $controller     =   $this->newController('LbsController');
        $info           =   $controller->removeUserLbsInfo($param);
        return  $info;
    }/*}}}*/
    # End Api   /*}}}*/
    
    # service logic/*{{{*/
    private function checkAdminDo($do)/*{{{*/
    {
        if (method_exists($this, $do))  return  TRUE;

        AC::$errorNo    =   'AA002';
        return  ' Error : call admin do is not exists! ';
    }/*}}}*/
    private function checkAdminUid($do)/*{{{*/
    {
        $unCheckAdminUid=   array(
                                'adminLogin'    =>  1,
                                'loginStatus'   =>  1,
                                #   'adminLogout'   =>  1,
                            );

        if (isset($unCheckAdminUid[$do]))   return  TRUE;
        return  $this->getAdminUser();
    }/*}}}*/
    private function formatData($do, $result)/*{{{*/
    {
        if (empty(AC::$errorNo))    AC::$errorNo    =   NULL;
        elseif ('adminLogin' != $do)
        {
            $data['param']  =   DOP::request();
        }

        $data['resultData'] =   $result;
        $data['serverTime'] =   AC::SERVER_TIME;
        $data['errorNo']    =   AC::$errorNo;
        $data['do']         =   $do;

        return  DOP::encodeData($data);
    }/*}}}*/
    private function formatHtml($do, $result)/*{{{*/
    {
        if (empty(AC::$errorNo))    AC::$errorNo    =   NULL;
        elseif ('adminLogin' != $do)
        {
            $data['param']  =   DOP::request();
        }

        if (!is_array($result))
        {
            $result =   '<tr><td>Result Data</td><td>'. $result. '</td><td>'. AC::$errorNo. '</td></tr>';
        }
        else
        {
            $i      =   0;
            $lable  =   NULL;
            $text   =   NULL;
            foreach ($result as $key => $value)
            {
                if (!is_array($value))
                {
                    $lable  .=  '<td>'. $key. '</td>';
                    $text   .=  '<td>'. $value. '</td>';
                }
                else
                {
                    $text[$i]   =   NULL;
                    foreach ($value as  $k =>   $v)
                    {
                        if (empty($i))  $lable  .=  '<td>'. $k. '</td>';
                        $text[$i]   .=  '<td>'. $v. '</td>';
                    }
                    $i++;
                }
            }
            if (is_array($text))
            {
                $result =   '<tr>'. $lable. '</tr><tr>'. implode('</tr><tr>', $text). '</tr>';
            }
            else
            {
                $result =   '<tr>'. $lable. '</tr><tr>'. $text. '</tr>';
            }
        }

        return  $result;
    }/*}}}*/
    # new controller    /*{{{*/
    private function newController($controller)
    {
        if (empty($controller))
        {
            AC::$errorNo    =   'AA003';
            return  FALSE;
        }
        if (isset(self::$controllerCache[$controller])  &&  is_resource(self::$controllerCache[$controller]))
        {
            return  self::$controllerCache[$controller];
        }
        self::$controllerCache[$controller] =   new $controller();
        return  self::$controllerCache[$controller];
    }/*}}}*/
    # end service logic/*}}}*/

}

